package com.tony.juc.ThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: NewSecheduledThreadPoolDemo001
 * @Auther: 皮蛋布丁
 * @Date: 2022/04/17/19:27
 * @Description: 定时调度线程池
 */
public class NewSecheduledThreadPoolDemo001 {

    public static void main(String[] args) {

        /*//5秒之后才会开始执行调度的任务，这个任务并不是直接执行的
        ScheduledExecutorService service = Executors.newScheduledThreadPool(6);
        for (int i = 0; i < 5; i++) {
            service.schedule(()->{
                System.out.printf("【%s】皮蛋布丁%n",Thread.currentThread().getName());
            },5, TimeUnit.SECONDS); //5秒后开始执行此任务
        }*/

        //间隔调度处理
        //这种调度的线程池和后面很多定时任务的开发是有关联的，这个定时任务是在spring开发框架、springboot
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 5; i++) { //创建5个线程
            service.scheduleAtFixedRate(()->{
                System.out.printf("【%s】皮蛋布丁%n",Thread.currentThread().getName());
            },5,2,TimeUnit.SECONDS); //初始延迟5秒，每2秒做一次调度
        }
    }
}
